/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
        if(head==nullptr||k==0) return nullptr;
        ListNode* pAhead=head;
        ListNode* pBehind=nullptr;
        for(int i=0;i<k-1;i++){
            if(pAhead->next){
                pAhead=pAhead->next;
            }else{
                return nullptr;
            }
        }
        pBehind=head;
        while(pAhead->next){
            pAhead=pAhead->next;
            pBehind=pBehind->next;
        }
        return pBehind;
    }
};